package com.inxedu.os.edu.controller.course;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.inxedu.os.common.ObjectUtils;
import com.inxedu.os.edu.pojo.CourseKpoint;
import com.inxedu.os.edu.pojo.CourseKpointDto;
import com.inxedu.os.edu.service.course.CourseKpointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
@Api(value = "课程管理（章节管理）", tags = "课程管理（章节管理")
public class AdminKpointController {

    public static Gson gson = (new GsonBuilder()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    @InitBinder("courseKpoint")
    public void initBinderCourseKpoint(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("courseKpoint.");
    }

    @Autowired
    CourseKpointService courseKpointService;

    /**
     * 课程的视频列表
     */
    @RequestMapping(value = "/kpoint/list/{course.courseId}",method = RequestMethod.GET)
    @ApiImplicitParam(name = "courseId", value = "课程的Id", required = true, dataType = "courseId")
    @ApiOperation(value = "课程的视频列表", notes = "课程的视频列表")
    public String showKpointList(HttpServletRequest request, @PathVariable("course.courseId") Integer courseId) {
        //根据id获取视频节点信息
        List<CourseKpoint> kpointList = courseKpointService.selectCourseKpoint(courseId);

        if (ObjectUtils.isNotNull(kpointList)) {
            for (CourseKpoint ck : kpointList) {
                ck.setContent("");
            }
        }

        request.setAttribute("kpointList", gson.toJson(kpointList));
        request.setAttribute("courseId", courseId);

        return "/view/inxedu/admin/kpoint/kpoint_list";
    }

    public Map<String, Object> setJson(boolean success, String message, Object entity) {
        Map<String, Object> json = new HashMap();
        json.put("success", success);
        json.put("message", message);
        json.put("entity", entity);
        return json;
    }
    /**
     * 创建节点视频
     *
     * @param courseKpoint
     * @return Map<String, Object>
     */
    @RequestMapping(value = "/kpoint/addkpoint",method = RequestMethod.POST)
    @ApiImplicitParam(name = "courseKpoint", value = "视频节点对象", required = true, dataType = "courseKpoint")
    @ApiOperation(value = "创建节点视频", notes = "创建节点视频")
    @ResponseBody
    public Map<String, Object> addKpoint(@ModelAttribute("courseKpoint") CourseKpoint courseKpoint) {

        Map<String, Object> json = new HashMap<String, Object>();
        courseKpoint.setAddTime(new Date());
        courseKpoint.setFree(true);//1免费
        courseKpoint.setPlayCount(0);//播放次数0
        courseKpointService.addCourseKpoint(courseKpoint);
        json = this.setJson(true, null, courseKpoint);

        return json;
    }

    /**
     * 通过过ID，查询章节详情
     * @param kpointId 章节ID
     * @return Map<String,Object>
     */
    @RequestMapping(value = "/kpoint/getkpoint/{kpointId}",method = RequestMethod.POST)
    @ApiImplicitParam(name = "kpointId", value = "视频节点Id", required = true, dataType = "kpointId")
    @ApiOperation(value = "查询章节详情", notes = "查询章节详情")
    @ResponseBody
    public Map<String,Object> queryCourseKpointById(@PathVariable("kpointId") int kpointId){
        Map<String,Object> json = new HashMap<String,Object>();

        try {
            CourseKpointDto kpoint = courseKpointService.queryCourseKpointById(kpointId);
            json = this.setJson(true,"成功",  kpoint);
        } catch (Exception e) {
            json = this.setJson(false, "失败", null);
            e.printStackTrace();

        }

        return json;
    }

    /**
     * 删除视频节点
     * @param kpointIds 视频ID串
     * @return Map<String,Object>
     */
    @RequestMapping(value = "/kpoint/deletekpoint/{kpointIds}",method = RequestMethod.POST)
    @ApiImplicitParam(name = "kpointIds", value = "视频节点Id串", required = true, dataType = "kpointIds")
    @ApiOperation(value = "删除视频节点", notes = "删除视频节点")
    @ResponseBody
    public Map<String,Object> deleteKpoint(@PathVariable("kpointIds") String kpointIds){
        Map<String,Object> json = new HashMap<String,Object>();

        try {
            courseKpointService.deleteKpointByIds(kpointIds);
            json = this.setJson(true, "删除成功", null);
        } catch (Exception e) {
            json = this.setJson(false, "删除失败", null);
            e.printStackTrace();
        }

        return json;

    }

    /**
     * 修改视频节点的父节点
     * @param parentId
     * @param kpointId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "视频节点父节点ID", required = true, dataType = "parentId"),
            @ApiImplicitParam(name = "kpointId", value = "视频节点ID", required = true, dataType = "kpointId")
    })

    @ApiOperation(value = "修改视频节点的父节点", notes = "修改视频节点的父节点")
    @RequestMapping(value = "/kpoint/updateparentid/{parentId}/{kpointId}",method = RequestMethod.POST)
    public Map<String,Object> updateKpointParentId(@PathVariable("parentId") int parentId,@PathVariable("kpointId") int kpointId){
        Map<String,Object> json = new HashMap<String,Object>();

        courseKpointService.updateKpointParentId(kpointId, parentId);
        json = this.setJson(true, null, null);

        return json;
    }



    /**
     * 修改视频节点
     * @param courseKpoint
     * @return Map<String,Object>
     */
    @RequestMapping(value = "/kpoint/updateKpoint",method = RequestMethod.POST)
    @ApiImplicitParam(name = "courseKpoint", value = "视频节点对象", required = true, dataType = "courseKpoint")
    @ApiOperation(value = "修改视频节点", notes = "修改视频节点")
    @ResponseBody
    public Map<String,Object> updateKpoint(@ModelAttribute("courseKpoint") CourseKpoint courseKpoint){
        Map<String,Object> json = new HashMap<String,Object>();

        try {
            courseKpointService.updateKpoint(courseKpoint);
            json = this.setJson(true, null, courseKpoint);


        } catch (Exception e) {
            e.printStackTrace();
        }


        return json;
    }







}
